﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.Windows.Forms.Integration;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	public partial class SystemViewForm : System.Windows.Forms.Form
	{
		public SystemViewForm(Autodesk.Revit.DB.Document document, Selection sel)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			this.doc = document;
			foreach (ElementId elementId in sel.GetElementIds())
			{
				this.elemSet.Insert(this.doc.GetElementById(elementId));
			}
			base.Icon = Resource.YArchitechIcon;
			this.BindListView();
			this.BindComboBox();
		}

		private void BindListView()
		{
			foreach (object obj in this.elemSet)
			{
				Parameter parameter = ((Element)obj).GetParameter(BuiltInParameter.RBS_SYSTEM_NAME_PARAM);
				if (parameter != null && parameter.HasValue)
				{
					string[] array = parameter.AsString().Split(new char[]
					{
						','
					});
					for (int i = 0; i < array.Count<string>(); i++)
					{
						if (!this.lstSysName.Items.Contains(array[i]))
						{
							this.lstSysName.Items.Add(array[i]);
						}
					}
				}
			}
			this.elemSet = this.GetAllElementSet(this.doc);
			if (this.lstSysName.Items.Count == 0)
			{
				this.GetSystemName(typeof(PipingSystem));
				this.GetSystemName(typeof(MechanicalSystem));
			}
			if (this.lstSysName.Items.Count > 0)
			{
				this.lstSysName.SelectedIndex = 0;
			}
		}

		private void GetSystemName(Type type)
		{
			foreach (Element elem in new FilteredElementCollector(this.doc).OfClass(type).ToElements())
			{
				if (elem.GetParameter(BuiltInParameter.RBS_SYSTEM_NAME_PARAM) != null)
				{
					this.lstSysName.Items.Add(elem.GetParameter(BuiltInParameter.RBS_SYSTEM_NAME_PARAM).AsString());
				}
			}
		}

		private void BindComboBox()
		{
			this.cmbDetailLevel.Items.Clear();
			this.cmbDetailLevel.Items.Add("粗略");
			this.cmbDetailLevel.Items.Add("中等");
			this.cmbDetailLevel.Items.Add("精细");
			this.cmbAngle.Items.Clear();
			this.cmbAngle.Items.Add("东南轴测");
			this.cmbAngle.Items.Add("西南轴测");
			this.cmbAngle.Items.Add("东北轴测");
			this.cmbAngle.Items.Add("西北轴测");
			this.cmbRatio.Items.Clear();
			foreach (string str in new string[]
			{
				"1",
				"2",
				"5",
				"10",
				"20",
				"50",
				"100",
				"200",
				"500",
				"1000",
				"2000",
				"5000"
			})
			{
				this.cmbRatio.Items.Add("1:" + str);
			}
			this.GetUserSetting();
			if (this.chkView3D.Checked)
			{
				this.label2.Enabled = true;
				this.label3.Enabled = true;
				this.label4.Enabled = true;
				this.label5.Enabled = true;
				this.chkViewAngle.Enabled = true;
				this.hyTxtViewName.Enabled = true;
				this.cmbAngle.Enabled = true;
				this.cmbDetailLevel.Enabled = true;
				this.cmbRatio.Enabled = true;
				return;
			}
			this.label2.Enabled = false;
			this.label3.Enabled = false;
			this.label4.Enabled = false;
			this.label5.Enabled = false;
			this.chkViewAngle.Enabled = false;
			this.hyTxtViewName.Enabled = false;
			this.cmbAngle.Enabled = false;
			this.cmbDetailLevel.Enabled = false;
			this.cmbRatio.Enabled = false;
		}

		private void GetUserSetting()
		{
			SystemViewSetParam systemViewText = new HVACUserSetting().GetSystemViewText();
			this.cmbDetailLevel.SelectedIndex = systemViewText.DetailLevel;
			this.cmbAngle.SelectedIndex = systemViewText.Angle;
			this.cmbRatio.SelectedIndex = systemViewText.Ratio;
			this.chkView3D.Checked = systemViewText.View3D;
			this.chkViewAngle.Checked = systemViewText.ViewAngle;
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			if (this.lstSysName.SelectedItem == null)
			{
				YJKMessageBox.Show("请选择一个系统");
				return;
			}
			if (this.chkView3D.Checked)
			{
				if (this.hyTxtViewName.Text.Trim() == "")
				{
					YJKMessageBox.Show("视图名称不能为空");
					this.hyTxtViewName.Focus();
					return;
				}
				string[] array = new string[]
				{
					"''",
					":",
					"{",
					"}",
					"[",
					"]",
					"|",
					";",
					"<",
					">",
					"?",
					"~",
					"`"
				};
				for (int i = 0; i < array.Length; i++)
				{
					if (this.hyTxtViewName.Text.Contains(array[i]))
					{
						YJKMessageBox.Show("视图名称不能包含特殊字符");
						this.hyTxtViewName.Focus();
						return;
					}
				}
			}
			this.UserSettingWrite();
			base.DialogResult = DialogResult.OK;
		}

		private void UserSettingWrite()
		{
			try
			{
				new HVACUserSetting().WriteSystemViewText(new SystemViewSetParam
				{
					DetailLevel = this.cmbDetailLevel.SelectedIndex,
					Angle = this.cmbAngle.SelectedIndex,
					Ratio = this.cmbRatio.SelectedIndex,
					View3D = this.chkView3D.Checked,
					ViewAngle = this.chkViewAngle.Checked
				});
			}
			catch (Exception)
			{
			}
		}

		public View3D GetView3D(string strSysName)
		{
			View3D result;
			try
			{
				FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.doc);
				filteredElementCollector.OfClass(typeof(View3D));
				foreach (Element element in filteredElementCollector)
				{
					if (element.Name == this.hyTxtViewName.Text.Trim())
					{
						if (YJKMessageBox.Show("三维视图“" + element.Name + "”已存在，是否覆盖？", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
						{
							return null;
						}
						this.doc.DeleteElement(element);
						break;
					}
				}
				this.doc.Regenerate();
				View3D view3DParam = this.GetView3DParam(this.doc);
				if (this.chkViewAngle.Checked)
				{
					view3DParam.HYSaveOrientationAndLock();
				}
				view3DParam.HideYJKElements(this.elemSet);
				ElementSet systemElementSet = this.GetSystemElementSet(this.doc, strSysName);
				if (!systemElementSet.IsEmpty)
				{
					view3DParam.UnHideYJKElements(systemElementSet);
				}
				systemElementSet.Dispose();
				view3DParam.Name = this.hyTxtViewName.Text.Trim();
				this.doc.Regenerate();
				result = view3DParam;
			}
			catch
			{
				result = null;
			}
			return result;
		}

		private View3D GetView3DParam(Autodesk.Revit.DB.Document doc)
		{
			View3D result;
			try
			{
				XYZ xyz = new XYZ(0.0, 0.0, 0.0);
				View3D view3D = doc.CreatHYView3D(XYZ.Zero);
				XYZ xyz2 = null;
				XYZ xyz3 = null;
				string a = this.cmbAngle.SelectedItem.ToString();
				if (!(a == "东南轴测"))
				{
					if (!(a == "西南轴测"))
					{
						if (!(a == "东北轴测"))
						{
							if (a == "西北轴测")
							{
								xyz2 = new XYZ(1.0, -1.0, 2.0);
								xyz3 = new XYZ(1.0, -1.0, -1.0);
							}
						}
						else
						{
							xyz2 = new XYZ(-1.0, -1.0, 2.0);
							xyz3 = new XYZ(-1.0, -1.0, -1.0);
						}
					}
					else
					{
						xyz2 = new XYZ(1.0, 1.0, 2.0);
						xyz3 = new XYZ(1.0, 1.0, -1.0);
					}
				}
				else
				{
					xyz2 = new XYZ(-1.0, 1.0, 2.0);
					xyz3 = new XYZ(-1.0, 1.0, -1.0);
				}
				ViewOrientation3D orientation = new ViewOrientation3D(xyz, xyz2, xyz3);
				view3D.SetOrientation(orientation);
				string a2 = this.cmbDetailLevel.SelectedItem.ToString();
				if (!(a2 == "粗略"))
				{
					if (!(a2 == "中等"))
					{
						if (a2 == "精细")
						{
							view3D.GetParameter(BuiltInParameter.VIEW_DETAIL_LEVEL).Set(3);
						}
					}
					else
					{
						view3D.GetParameter(BuiltInParameter.VIEW_DETAIL_LEVEL).Set(2);
					}
				}
				else
				{
					view3D.GetParameter(BuiltInParameter.VIEW_DETAIL_LEVEL).Set(1);
				}
				string s = this.cmbRatio.SelectedItem.ToString().Replace("1:", "");
				view3D.GetParameter(BuiltInParameter.VIEW_SCALE_PULLDOWN_METRIC).Set(int.Parse(s));
				doc.Regenerate();
				result = view3D;
			}
			catch (Exception ex)
			{
				ex.ToString();
				result = null;
			}
			return result;
		}

		private ElementSet GetAllElementSet(Autodesk.Revit.DB.Document doc)
		{
			ElementSet elementSet = doc.Application.Create.NewElementSet();
			FilteredElementIterator elementIterator = new HYSystemElementFilter(doc).GetFilterByCondition(null, false, null).GetElementIterator();
			while (elementIterator.MoveNext())
			{
				Element element = elementIterator.Current;
				elementSet.Insert(element);
			}
			return elementSet;
		}

		private ElementSet GetSystemElementSet(Autodesk.Revit.DB.Document doc, string strSysName)
		{
			new HYSystemElementFilter(doc);
			return this.GetElemSetByParamValue((BuiltInParameter)(-1140324), strSysName);
		}

		public ElementSet GetElemSetByParamValue(BuiltInParameter biParam, string paramValue)
		{
			HYSystemElementFilter hysystemElementFilter = new HYSystemElementFilter(this.doc);
			ElementSet elementSet = this.doc.Application.Create.NewElementSet();
			FilteredElementIterator elementIterator = hysystemElementFilter.GetFilterByCondition(null, false, null).GetElementIterator();
			elementIterator.Reset();
			while (elementIterator.MoveNext())
			{
				Element element = elementIterator.Current;
				if (element != null)
				{
					Parameter parameter = element.GetParameter(biParam);
					if (parameter != null && parameter.HasValue && parameter.AsString().Trim() != "")
					{
						string[] array = parameter.AsString().Split(new char[]
						{
							','
						});
						for (int i = 0; i < array.Count<string>(); i++)
						{
							if (array[i].Equals(paramValue))
							{
								elementSet.Insert(element);
							}
						}
					}
				}
			}
			return elementSet;
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void lstSysName_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.lstSysName.SelectedItem == null)
			{
				return;
			}
			string text = this.lstSysName.SelectedItem.ToString();
			this.hyTxtViewName.Text = text;
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.doc);
			filteredElementCollector.OfClass(typeof(View3D));
			foreach (Element element in filteredElementCollector)
			{
				if (element.Name == "三维视图预览")
				{
					this.doc.DeleteElement(element);
					break;
				}
			}
			XYZ eye = new XYZ(0.0, 0.0, 0.0);
			View3D view3D = this.doc.CreatHYView3DSystem(new XYZ(1.0, 1.0, -1.0), eye);
			view3D.HideYJKElements(this.elemSet);
			ElementSet systemElementSet = this.GetSystemElementSet(this.doc, text);
			if (!systemElementSet.IsEmpty)
			{
				view3D.UnHideYJKElements(systemElementSet);
			}
			systemElementSet.Dispose();
			view3D.Name = "三维视图预览";
			this.doc.Regenerate();
			PreviewControl previewControl = this.elemHost.Child as PreviewControl;
			if (previewControl != null)
			{
				previewControl.Dispose();
			}
			this.elemHost.Child = new PreviewControl(this.doc, view3D.Id);
			this.doc.Regenerate();
		}

		private void chkView3D_CheckedChanged(object sender, EventArgs e)
		{
			if (this.chkView3D.Checked)
			{
				this.label2.Enabled = true;
				this.label3.Enabled = true;
				this.label4.Enabled = true;
				this.label5.Enabled = true;
				this.chkViewAngle.Enabled = true;
				this.hyTxtViewName.Enabled = true;
				this.cmbAngle.Enabled = true;
				this.cmbDetailLevel.Enabled = true;
				this.cmbRatio.Enabled = true;
				return;
			}
			this.label2.Enabled = false;
			this.label3.Enabled = false;
			this.label4.Enabled = false;
			this.label5.Enabled = false;
			this.chkViewAngle.Enabled = false;
			this.hyTxtViewName.Enabled = false;
			this.cmbAngle.Enabled = false;
			this.cmbDetailLevel.Enabled = false;
			this.cmbRatio.Enabled = false;
		}

		private Document doc;

		private ElementSet elemSet = new ElementSet();
	}
}
